package com.ruoyi.building.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.domain.AmProject;
import com.ruoyi.project.mapper.AmProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.building.mapper.AmBuildingMapper;
import com.ruoyi.building.domain.AmBuilding;
import com.ruoyi.building.service.IAmBuildingService;

/**
 * 楼宇管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-29
 */
@Service
public class AmBuildingServiceImpl implements IAmBuildingService 
{
    @Autowired
    private AmBuildingMapper amBuildingMapper;


    @Autowired
    private AmProjectMapper projectMapper;

    /**
     * 查询楼宇管理
     * 
     * @param id 楼宇管理ID
     * @return 楼宇管理
     */
    @Override
    public AmBuilding selectAmBuildingById(Long id)
    {
        return amBuildingMapper.selectAmBuildingById(id);
    }

    /**
     * 查询楼宇管理列表
     * 
     * @param amBuilding 楼宇管理
     * @return 楼宇管理
     */
    @Override
    public List<AmBuilding> selectAmBuildingList(AmBuilding amBuilding)
    {
        amBuilding.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        return amBuildingMapper.selectAmBuildingList(amBuilding);
    }

    /**
     * 根据项目id查询楼宇
     *
     * @param id
     * @return 楼宇管理集合
     */
    @Override
    public List<HashMap<String, Object>> listByProject(Long id) {
        return amBuildingMapper.listByProject(id);
    }

    /**
     * 新增楼宇管理
     * 
     * @param amBuilding 楼宇管理
     * @return 结果
     */
    @Override
    public int insertAmBuilding(AmBuilding amBuilding)
    {
        AmProject project = projectMapper.selectAmProjectById(amBuilding.getProjectId());
        List<String> list = amBuildingMapper.selectAllAmBuildingList();
        if (list.contains(project.getProjectName()+amBuilding.getBuildingName())){
            throw new CustomException("该楼宇已经存在,不可重复添加");
        }
        amBuilding.setCreateTime(DateUtils.getNowDate());
        return amBuildingMapper.insertAmBuilding(amBuilding);
    }

    /**
     * 楼宇导入
     *
     * @param list
     * @param updateSupport
     */
    @Override
    public String importProject(List<AmBuilding> list, boolean updateSupport) {
        //查询所有项目
        List<AmProject> projects = projectMapper.selectAmProjectList(null);
        list.forEach(amBuilding -> {
            amBuilding.setProjectId(AmProject.castIdByName(projects,amBuilding.getProjectName()));
            amBuildingMapper.insertAmBuilding(amBuilding);
        });
        return "插入成功";
    }

    /**
     * 修改楼宇管理
     * 
     * @param amBuilding 楼宇管理
     * @return 结果
     */
    @Override
    public int updateAmBuilding(AmBuilding amBuilding)
    {
        AmProject project = projectMapper.selectAmProjectById(amBuilding.getProjectId());
        List<String> list = amBuildingMapper.selectAllAmBuildingList();
        if (list.contains(project.getProjectName()+amBuilding.getBuildingName())){
            throw new CustomException("该楼宇已经存在,不可重复添加");
        }
        return amBuildingMapper.updateAmBuilding(amBuilding);
    }

    /**
     * 批量删除楼宇管理
     * 
     * @param ids 需要删除的楼宇管理ID
     * @return 结果
     */
    @Override
    public int deleteAmBuildingByIds(Long[] ids)
    {
        return amBuildingMapper.deleteAmBuildingByIds(ids);
    }

    /**
     * 删除楼宇管理信息
     * 
     * @param id 楼宇管理ID
     * @return 结果
     */
    @Override
    public int deleteAmBuildingById(Long id)
    {
        return amBuildingMapper.deleteAmBuildingById(id);
    }

    /***
     * 获取楼宇楼层
     * @param buildingId
     */
    @Override
    public List<Map<String, Object>> getFloorList(Long buildingId) {
        AmBuilding amBuilding = amBuildingMapper.selectAmBuildingById(buildingId);
        List<Map<String ,Object> > list = new ArrayList<Map<String, Object>>();
        for (Long  i =amBuilding.getFloorStart() ; i<=amBuilding.getFloorEnd()  ; i++){
            if ( i.equals(0L)){
                continue;
            }
         Map<String,Object> map = new HashMap<String,Object>(16);
            map.put("id",i);
            map.put("name", i +"层");
            list.add(map);
        }
        return list;
    }

    /**
     * 获取所有的楼宇名称
     */
    @Override
    public List<String> selectAllAmBuildingList() {
        return amBuildingMapper.selectAllAmBuildingList();
    }
}
